import tkinter as tk
from tkinter import ttk
from tkinter import simpledialog,filedialog
from datetime import datetime
from PIL import Image, ImageTk

window=tk.Tk()
window.title("To Do List")
window.geometry("800x600")
columns=["To_Do","In Progress","Done"]
frames={}
for col in columns:
    frame = tk.Frame(window, relief=tk.RAISED, borderwidth=1)
    frame.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)
    frames[col] = frame
    label = tk.Label(frame, text=col, bg='lightblue', font=('Arial', 12, 'bold'))
    label.pack(fill=tk.X)
def add_task(task_name, priority, due_date, image_path,dialog):
    # 检查任务名称、优先级和截止日期是否都已提供
    if not task_name or not priority or not due_date:
        # 如果未提供所有必要信息，则不执行任何操作并返回
        return
        # 创建任务卡片
    # 在"To_Do"框架中创建任务卡片
    create_card(frames["To_Do"], task_name, priority, due_date,image_path)
    # 关闭对话框
    dialog.destroy()
def create_card_dialog():
    # 创建一个新的顶级窗口
    dialog = tk.Toplevel(window)
    dialog.title("Create New Task")
    dialog.geometry("400x300")
    
    # 输入框变量
    task_name_var = tk.StringVar()
    priority_var = tk.StringVar()
    due_data_var = tk.StringVar()
    image_path_var = tk.StringVar()

    # 任务名称
    # 任务名称标签
    task_name_label = tk.Label(dialog, text="Task Name:")
    task_name_label.pack(pady=5)
    # 任务名称输入框
    task_name_entry = tk.Entry(dialog, textvariable=task_name_var)
    task_name_entry.pack(pady=5)
    
    # 优先级
    # 优先级标签
    priority_label = tk.Label(dialog, text="Priority:")
    priority_label.pack(pady=5)
    # 优先级输入框
    priority_entry = tk.Entry(dialog, textvariable=priority_var)
    priority_entry.pack(pady=5)
    
    # 截止日期
    # 截止日期标签
    due_data_label = tk.Label(dialog, text="Due Date:")
    due_data_label.pack(pady=5)
    # 截止日期输入框
    due_data_entry = tk.Entry(dialog, textvariable=due_data_var)
    due_data_entry.pack(pady=5)

    def select_image():
        # 文件选择对话框，选择图片文件
        filename = filedialog.askopenfilename(title="Select Image", filetypes=[("Image Files", "*.png;*.jpg;*.jpeg;*.gif")])
        if filename:
            image_path_var.set(filename)
    
    # 确认按钮
    # 确认按钮，调用add_task函数并关闭对话框
    confirm_button = tk.Button(dialog, text="Confirm", command=lambda: add_task(task_name_var.get(), priority_var.get(), due_data_var.get(),image_path_var.get(), dialog))
    confirm_button.pack(pady=10)
    # 选择图片按钮
    select_image_button = tk.Button(dialog, text="Select Image", command=select_image)
    select_image_button.pack(pady=5)
   


def create_card(frame, task_name, priority, due_data,image_path):
    # 创建一个新的框架用于显示卡片
    card_frame = tk.Frame(frame, relief=tk.RAISED, borderwidth=1, padx=10, pady=10)
    card_frame.pack(fill=tk.X, pady=5)
    
    # 创建任务名称标签
    task_label = tk.Label(card_frame, text=f"Task: {task_name}", anchor='w')
    task_label.pack(fill=tk.X)

    # 创建优先级标签
    priority_label = tk.Label(card_frame, text=f"Priority: {priority}", anchor='w')
    priority_label.pack(fill=tk.X)

    # 创建截止日期标签
    due_data_label = tk.Label(card_frame, text=f"Due data: {due_data}", anchor='w')
    due_data_label.pack(fill=tk.X)

    # 创建编辑按钮
    edit_button = tk.Button(card_frame, text="edit", command=lambda:edit_task(task_name,card_frame))
    edit_button.pack(anchor='s')

    # 创建删除按钮
    delete_button = tk.Button(card_frame, text="delete", command=lambda: delete_card(card_frame))
    delete_button.pack(anchor='s')

    # 创建创建时间标签
    created_time_label = tk.Label(card_frame, text=f"Created: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}", anchor='w')
    created_time_label.pack(fill=tk.X)

    # 如果存在图片路径，则加载并显示图片
    if image_path:
        img = Image.open(image_path)
        img = img.resize((100, 100)) 
        img = ImageTk.PhotoImage(img)
        image_label = tk.Label(card_frame, image=img)
        image_label.image = img  
        image_label.pack()
    
def delete_card(card_frame):
    # 删除卡片框架
    card_frame.destroy()


def edit_task(task_name, card_frame):
    # 弹出对话框询问用户输入新的任务名称
    new_task_name = simpledialog.askstring("Edit task name", f"Current task: {task_name}\nPlease enter new task name")
    # 如果用户输入了新的任务名称
    if new_task_name:
        
        # 遍历card_frame中的所有子组件
        for widget in card_frame.winfo_children():
            # 如果组件是Label类型且文本以"Task:"开头
            if isinstance(widget, tk.Label) and widget.cget('text').startswith("Task:"):
                # 更新Label的文本为新任务名称
                widget.config(text=f"Task: {new_task_name}")


button=tk.Button(window,text="create",command=create_card_dialog)
button.pack()
window.mainloop()